package com.stone.im.dispatcher;
import io.netty.channel.socket.SocketChannel;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 接入系统的网络连接及管理组件
 */
public class GatewayInstanceManager {

    private GatewayInstanceManager(){}

    /**
     * 单例
     */
    static class Singleton{
        private static GatewayInstanceManager instance = new GatewayInstanceManager();
    }

    /**
     * 接入系统的实例列表
     */
    private ConcurrentHashMap<String, SocketChannel> gatewayInstances = new ConcurrentHashMap<>();

    /**
     * 获取单例
     * @return
     */
    public static GatewayInstanceManager getInstance(){
        return Singleton.instance;
    }

    /**
     * 添加一个接入系统实例
     * @param channelId 网络连接id
     * @param channel   网络连接channel
     */
    public  void addGatewayInstance(String channelId,SocketChannel channel){
        gatewayInstances.put(channelId,channel);
    }

    /**
     * 删除一个接入系统实例
     * @param channelId
     */
    public  void removeGatewayInstance(String channelId){
        gatewayInstances.remove(channelId);
    }

    /**
     * 获取实例
     * @param key
     * @return
     */
    public SocketChannel getGatewayInstance(String key){
        return gatewayInstances.get(key);
    }
    /**
     * 初始化组件
     */
    public void init(){

        /**
         * 接入系统主动跟分发系统建立连接
         * 建立好连接可以吧接入系统的长连接缓存到这个组件
         */

    }


}
